<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use api\wxapp\model\Order;
use api\wxapp\model\Refund;
use api\wxapp\model\User;
use api\wxapp\model\Shop;
use api\wxapp\model\Goods;
use api\wxapp\model\Coupon;
class RefundController extends AdminBaseController
{

    public function index(Refund $refund)
    {
        $shop_id = 0;
        $params = $this->param();
        $where = array();
        $sessionAdminId = session('ADMIN_ID');
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $id = Shop::where('admin_id',$sessionAdminId)->value('id');
            $shop_id = $id;
            $idarr = User::where('shop_id',$id)->column('id');
            $where[] = ['t2.uid','in',$idarr];
        }
        if(isset($params['name']) && $params['name']){
            $map[] = ['name','like','%'.$params['name'].'%'];
            $sid = Shop::where($map)->column('id');
            $where[] = ['t2.shop_id','in',$sid];
        }
        if(isset($params['nickname'])){
            $where[] = ['t3.nickname','like','%'.$params['nickname'].'%'];
        }
        $query = Refund::alias('t1');
        $query = $refund->leftorder($query);
        $query = $refund->leftuser($query);
        $query = $refund->returnfield($query);
        $list = $query->where($where)->paginate(10,false,
        ['query'=>$params])->each(function($item, $key){
            $item['img'] = json_decode($item['img']);
            if($item['is_coupon']){
                $coupon = Coupon::get($item['is_coupon']);
                $item['is_coupon'] = '满'.$coupon['full'].'减'.$coupon['reduce'];
            }else{
                $item['is_coupon'] = '无';
            }
            if($item['goods_id']){
                $goods_id = explode(',',$item['goods_id']);
                $str = '';
                foreach($goods_id as $k=>$v){
                    $str .= Goods::where('id',$v)->value('name').',';
                }
                $item['goods_name'] = $str;
            }else{
                $item['goods_name'] = '全部';
            }
            $item['shopname'] = Shop::where('id',$item['shop_id'])->value('name');
            return $item;
        });
        $this->assign('shop_id',$shop_id);
        $this->assign('list',$list);
        $list->appends($params);  //分页条件
        $this->assign('page', $list->render());
        return $this->fetch();
    }
    public function edit()
    {
        $params = $this->param();
        $res = Refund::update($params);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
}
